const fs = require('fs');
const path = require('path');

// 生成 writeStream
function createWriteStream(fileName) {
  const fullFileName = path.join(__dirname, `../../logs/${fileName}`);
  const writeStream = fs.createWriteStream(fullFileName, {
    flags: 'a'
  });
  return writeStream;
}

// 写访问日志
function writeLog(writeStream, log) {
  writeStream.write(log + '\n');
}

// access.log 的流对象
const accessWriteStream = createWriteStream('access.log');
function access(req) {
  const agent = req.headers['user-agent'];
  const time = Date.now();
  const log = `${req.method} -- ${req.url} -- ${agent} -- ${time}`;
  writeLog(accessWriteStream, log);
}

module.exports = {
  access
}
